/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Form;

import BLL.BLLManu;
import BLL.BLLProduct;
import DTO.DTOProduct;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.text.NumberFormat;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Pham
 */
public class JPanelManageProduct extends javax.swing.JPanel {
    BLLManu bllManu;
    BLLProduct bllPro;
    JFileChooser fileChooserImage;
    File fileImage;
    NumberFormat numberformat;
    /**
     * Creates new form NewJPanel
     */
    public JPanelManageProduct() {
        bllManu = new BLLManu();
        bllPro = new BLLProduct();
        //load componets
        initComponents();
        
        //load data
        
        loadDatatoTable();
        DefaultComboBoxModel cbModel = bllManu.getDcbModelManu();
        cbSearchManu.setModel(cbModel);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private void initComponents() {//GEN-BEGIN:initComponents

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel2 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jLabel11 = new javax.swing.JLabel();
        txtSearchName = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        cbSearchManu = new javax.swing.JComboBox();
        jLabel13 = new javax.swing.JLabel();
        txtSearchFrom = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        txtSearchPriceTo = new javax.swing.JTextField();
        btnSearch = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        tbProData = new javax.swing.JTable();
        btnLoadData = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        btnAddNew = new javax.swing.JButton();
        bnEdit = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Danh sách sản phẩm", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(0, 0, 102)));

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Tìm kiếm", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(102, 0, 0)));

        jLabel11.setText("Tên SP");

        jLabel12.setText("Hãng SX");

        jLabel13.setText("Từ giá");

        jLabel14.setText("Đến giá");

        btnSearch.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnSearch.setForeground(new java.awt.Color(102, 0, 51));
        btnSearch.setText("Tìm");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel11)
                    .addComponent(jLabel13)
                    .addComponent(jLabel12)
                    .addComponent(jLabel14))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtSearchName, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbSearchManu, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtSearchFrom)
                    .addComponent(txtSearchPriceTo)
                    .addComponent(btnSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(txtSearchName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(cbSearchManu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(txtSearchFrom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel14)
                    .addComponent(txtSearchPriceTo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(21, Short.MAX_VALUE))
        );

        tbProData.setAutoCreateRowSorter(true);
        tbProData.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbProData.setRowSorter(null);
        tbProData.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tbProDataMousePressed(evt);
            }
        });
        jScrollPane3.setViewportView(tbProData);

        btnLoadData.setBackground(new java.awt.Color(0, 204, 102));
        btnLoadData.setText("Tải danh sách");
        btnLoadData.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoadDataActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(btnLoadData)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 615, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(btnLoadData)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 148, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Chức năng", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(0, 102, 0)));

        btnAddNew.setBackground(new java.awt.Color(204, 204, 204));
        btnAddNew.setText("Thêm mới");
        btnAddNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddNewActionPerformed(evt);
            }
        });

        bnEdit.setBackground(new java.awt.Color(153, 153, 255));
        bnEdit.setText("Sửa");
        bnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bnEditActionPerformed(evt);
            }
        });

        btnDelete.setBackground(new java.awt.Color(255, 153, 153));
        btnDelete.setText("Xóa");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnAddNew)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAddNew)
                    .addComponent(bnEdit)
                    .addComponent(btnDelete))
                .addGap(0, 11, Short.MAX_VALUE))
        );

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(153, 0, 0));
        jLabel1.setText("Quản lý sản phẩm");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(337, 337, 337)
                        .addComponent(jLabel1)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }//GEN-END:initComponents

    //method action search product and load data to table
    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        try{
            String name = txtSearchName.getText().trim();
            String manu = cbSearchManu.getSelectedItem().toString();
            long priceFrom = 0;
            long priceTo = 0;
            if(!"".equals(txtSearchFrom.getText().trim()))
            priceFrom = Long.valueOf(txtSearchFrom.getText());
            if(!"".equals(txtSearchPriceTo.getText().trim()))
            priceTo = Long.valueOf(txtSearchPriceTo.getText());
            DefaultTableModel model = bllPro.searchProductModel(name, manu, priceFrom, priceTo);
            tbProData.setModel(model);
        }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_btnSearchActionPerformed

    //method action press button loaddata to load data to table
    private void btnLoadDataActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoadDataActionPerformed
        loadDatatoTable();
    }//GEN-LAST:event_btnLoadDataActionPerformed

    //method row selected change
    private void tbProDataMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbProDataMousePressed
        /*try
        {
            int ProID = Integer.valueOf(tbProData.getValueAt(tbProData.getSelectedRow(), 0).toString());
            DTO.DTOProduct newDTOPro = bllPro.getProductbyID(ProID);
            if(newDTOPro != null){
                
                lbID.setText(String.valueOf(newDTOPro.getProductID()));
                txtProName.setText(String.valueOf(newDTOPro.getProductName()));
                cbManu.setSelectedItem(newDTOPro.getManu());
                txtProPrice.setText(String.valueOf(newDTOPro.getProductPrice()));
                ImageIcon icon = new ImageIcon("D:\\wamp\\www\\shopcomputer\\public\\default\\images\\products\\" + String.valueOf(newDTOPro.getProductImage()));
                if(icon != null)
                    lbImage.setIcon(resizeImg(icon, 70, 70));
                txtProWarranty.setText(String.valueOf(newDTOPro.getProductWarranty()));
                txtProPromotion.setText(String.valueOf(newDTOPro.getProductPromotion()));
                txtProDescription.setText(String.valueOf(newDTOPro.getProductDescription()));
            }
            
        }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null, "Error" + ex.getMessage());
        }*/
        
    }//GEN-LAST:event_tbProDataMousePressed

    //method action save product
    private void bnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnEditActionPerformed
        int row = tbProData.getSelectedRow();
        if(row != -1)
        {
            int ProID = Integer.valueOf(tbProData.getValueAt(tbProData.getSelectedRow(), 0).toString());
            JDialogProduct dialog = new JDialogProduct(null, true, ProID);
            Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
            dialog.setLocation((dim.width - dialog.getWidth())/2, (dim.height - dialog.getHeight())/2);
            
            dialog.addWindowListener(new WindowListener() {

                @Override
                public void windowOpened(WindowEvent e) {
                    
                }

                @Override
                public void windowClosing(WindowEvent e) {
                    
                }

                @Override
                public void windowClosed(WindowEvent e) {
                    loadDatatoTable();
                }

                @Override
                public void windowIconified(WindowEvent e) {
                    
                }

                @Override
                public void windowDeiconified(WindowEvent e) {
                    
                }

                @Override
                public void windowActivated(WindowEvent e) {
                    
                }

                @Override
                public void windowDeactivated(WindowEvent e) {
                   
                }
            });
            dialog.setVisible(true); 
        }
        else
            JOptionPane.showMessageDialog(null, "Vui lòng chọn sản phẩm muốn sửa");
           
    }//GEN-LAST:event_bnEditActionPerformed

    //method action clear all field to input new product
    private void btnAddNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddNewActionPerformed
        JDialogProduct dialog = new JDialogProduct(null, true, -1);
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        dialog.setLocation((dim.width - dialog.getWidth())/2, (dim.height - dialog.getHeight())/2);
        dialog.addWindowListener(new WindowListener() {

                @Override
                public void windowOpened(WindowEvent e) {
                    
                }

                @Override
                public void windowClosing(WindowEvent e) {
                    
                }

                @Override
                public void windowClosed(WindowEvent e) {
                    loadDatatoTable();
                }

                @Override
                public void windowIconified(WindowEvent e) {
                    
                }

                @Override
                public void windowDeiconified(WindowEvent e) {
                    
                }

                @Override
                public void windowActivated(WindowEvent e) {
                    
                }

                @Override
                public void windowDeactivated(WindowEvent e) {
                   
                }
            });
        dialog.setVisible(true);
        
    }//GEN-LAST:event_btnAddNewActionPerformed

    //method action delete product
    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        try
        {
            int rowcheck = tbProData.getSelectedRow();
            if(rowcheck > -1)
            {
                int quesstion = JOptionPane.showConfirmDialog(this, "Bạn có chắc muốn xóa?", "Thông báo", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                if(quesstion == JOptionPane.YES_OPTION)
                {

                        DefaultTableModel model = (DefaultTableModel)tbProData.getModel();
                        int[] rows = tbProData.getSelectedRows();
                        for(int i = rows.length - 1; i >= 0; i--)
                        {
                            int id = (int)tbProData.getModel().getValueAt(rows[i], 0);
                            DTOProduct dtoPro = bllPro.getProductbyID(id);
                            if(dtoPro != null)
                            {
                                if(bllPro.deleteRowProduct(dtoPro))
                                {
                                    model.removeRow(rows[i]);
                                    bllPro.deleteImage("D:\\wamp\\www\\shopcomputer\\public\\default\\images\\products\\" + dtoPro.getProductImage());
        //                                JOptionPane.showMessageDialog(null, "Không thể xóa hình");
                                }
                                else
                                    JOptionPane.showMessageDialog(null, "không thể xóa");
                            }

                        }
                        
                    }
                    
            }
            else
            {
                JOptionPane.showMessageDialog(null, "vui lòng chọn sản phẩm muốn xóa");
            }
            
        }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null, "Error: " + ex.getMessage());
        }
    }//GEN-LAST:event_btnDeleteActionPerformed

    
    //method load data to table
    private void loadDatatoTable()
    {
        DefaultTableModel model = bllPro.getProductModel();
        tbProData.setModel(model);
    }
    
    //resize image
    private ImageIcon resizeImg(ImageIcon iconIn, int width, int height)
    {
        Image img = iconIn.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH);
        ImageIcon iconOut = new ImageIcon(img);
        return iconOut;
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bnEdit;
    private javax.swing.JButton btnAddNew;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnLoadData;
    private javax.swing.JButton btnSearch;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JComboBox cbSearchManu;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable tbProData;
    private javax.swing.JTextField txtSearchFrom;
    private javax.swing.JTextField txtSearchName;
    private javax.swing.JTextField txtSearchPriceTo;
    // End of variables declaration//GEN-END:variables
}
